package com.lqzs.businessmanager.utils

import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.provider.Settings.SettingNotFoundException
import android.text.TextUtils
import android.text.TextUtils.SimpleStringSplitter
import android.util.Log
import com.lqzs.businessmanager.service.AccessibilitySampleService

/**
 * 判断是否有辅助功能权限
 */
fun isAccessibilitySettingsOn(context: Context?):Boolean{
    if (context == null)
        return false

    var accessibilityEnabled = 0
    try {
        accessibilityEnabled = Settings.Secure.getInt(
            context.applicationContext.contentResolver,
            Settings.Secure.ACCESSIBILITY_ENABLED
        )
    } catch (e: SettingNotFoundException) {
        e.printStackTrace()
    }

    val packageName = context.packageName
    val serviceStr = packageName + "/" + AccessibilityUtil.ACCESSIBILITY_SERVICE_PATH
    Log.e("ACC","serviceStr: $serviceStr")
    if (accessibilityEnabled == 1) {
        val mStringColonSplitter = SimpleStringSplitter(':')

        val settingValue = Settings.Secure.getString(
            context.applicationContext.contentResolver,
            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
        )
        if (settingValue != null) {
            mStringColonSplitter.setString(settingValue)
            while (mStringColonSplitter.hasNext()) {
                val accessabilityService = mStringColonSplitter.next()

                if (accessabilityService.equals(serviceStr, ignoreCase = true)) {
                    return true
                }
            }
        }
    }
    return false
}

/**
 * 判断是否有辅助功能权限
 */
fun isOpenAccessibility(context: Context): Boolean {
    try {
        val service =
            context.packageName + "/" + AccessibilitySampleService::class.java.canonicalName
        val accessibility = Settings.Secure.getInt(
            context.applicationContext.contentResolver,
            Settings.Secure.ACCESSIBILITY_ENABLED
        )
        val stringSplitter = SimpleStringSplitter(':')
        if (accessibility == 1) {
            val value = Settings.Secure.getString(
                context.applicationContext.contentResolver,
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
            )
            if (!TextUtils.isEmpty(value)) {
                stringSplitter.setString(value)
                while (stringSplitter.hasNext()) {
                    val next = stringSplitter.next()
                    if (next.equals(service, ignoreCase = true)) {
                        return true
                    }
                }
            }
        }
    } catch (e: SettingNotFoundException) {
        e.printStackTrace()
    }
    return false
}

fun getAccessibilitySettingPageIntent(context: Context?): Intent {
    // 一些品牌的手机可能不是这个Intent,需要适配
    return Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
}

class AccessibilityUtil {

    companion object{
        val ACCESSIBILITY_SERVICE_PATH: String = AccessibilitySampleService::class.java.getCanonicalName()!!.toString()
    }


}